// 页面设置
import defaultSettings from '@/settings'
import Cookies from 'js-cookie'

const themeDefaultColor = '#409eff'

const { showSettings, fixedHeader, sidebarLogo } = defaultSettings

const state = {
  // showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  theme: Cookies.get('theme-color') || themeDefaultColor
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
    // 如果修改的是 theme
    if (key === 'theme') {
      // 将颜色持久化到本地
      Cookies.set('theme-color', value)
    }
  },
  resetTheme(state) {
    // 重置 vuex 主题色
    state.theme = themeDefaultColor
    // 清空本地存储主题色
    Cookies.remove('theme-color')
  }
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

